package com.sun.mail.pop3;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.FolderClosedException;
import javax.mail.IllegalWriteException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class POP3Message extends MimeMessage {
    static final String UNKNOWN = "UNKNOWN";
    private POP3Folder folder;
    private int hdrSize = -1;
    private int msgSize = -1;
    String uid = UNKNOWN;

    public POP3Message(Folder folder, int msgno) throws MessagingException {
        super(folder, msgno);
        this.folder = (POP3Folder) folder;
    }

    public void setFlags(Flags newFlags, boolean set) throws MessagingException {
        Flags oldFlags = (Flags) this.flags.clone();
        super.setFlags(newFlags, set);
        if (!this.flags.equals(oldFlags)) {
            this.folder.notifyMessageChangedListeners(1, this);
        }
    }

    public int getSize() throws MessagingException {
        try {
            int i;
            synchronized (this) {
                if (this.msgSize >= 0) {
                    i = this.msgSize;
                } else {
                    if (this.msgSize < 0) {
                        if (this.headers == null) {
                            loadHeaders();
                        }
                        if (this.contentStream != null) {
                            this.msgSize = this.contentStream.available();
                        } else {
                            this.msgSize = this.folder.getProtocol().list(this.msgnum) - this.hdrSize;
                        }
                    }
                    i = this.msgSize;
                }
            }
            return i;
        } catch (EOFException eex) {
            this.folder.close(false);
            throw new FolderClosedException(this.folder, eex.toString());
        } catch (IOException ex) {
            throw new MessagingException("error getting size", ex);
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    protected java.io.InputStream getContentStream() throws javax.mail.MessagingException {
        /*
        r14 = this;
        r13 = 10;
        r9 = 0;
        monitor-enter(r14);	 Catch:{ EOFException -> 0x002b, IOException -> 0x00ae }
        r8 = r14.contentStream;	 Catch:{ all -> 0x0028 }
        if (r8 != 0) goto L_0x006e;
    L_0x0008:
        r8 = r14.folder;	 Catch:{ all -> 0x0028 }
        r10 = r8.getProtocol();	 Catch:{ all -> 0x0028 }
        r11 = r14.msgnum;	 Catch:{ all -> 0x0028 }
        r8 = r14.msgSize;	 Catch:{ all -> 0x0028 }
        if (r8 <= 0) goto L_0x003d;
    L_0x0014:
        r8 = r14.msgSize;	 Catch:{ all -> 0x0028 }
        r12 = r14.hdrSize;	 Catch:{ all -> 0x0028 }
        r8 = r8 + r12;
    L_0x0019:
        r7 = r10.retr(r11, r8);	 Catch:{ all -> 0x0028 }
        if (r7 != 0) goto L_0x003f;
    L_0x001f:
        r8 = 1;
        r14.expunged = r8;	 Catch:{ all -> 0x0028 }
        r8 = new javax.mail.MessageRemovedException;	 Catch:{ all -> 0x0028 }
        r8.<init>();	 Catch:{ all -> 0x0028 }
        throw r8;	 Catch:{ all -> 0x0028 }
    L_0x0028:
        r8 = move-exception;
        monitor-exit(r14);	 Catch:{ all -> 0x0028 }
        throw r8;	 Catch:{ EOFException -> 0x002b, IOException -> 0x00ae }
    L_0x002b:
        r3 = move-exception;
        r8 = r14.folder;
        r8.close(r9);
        r8 = new javax.mail.FolderClosedException;
        r9 = r14.folder;
        r10 = r3.toString();
        r8.<init>(r9, r10);
        throw r8;
    L_0x003d:
        r8 = r9;
        goto L_0x0019;
    L_0x003f:
        r8 = r14.headers;	 Catch:{ all -> 0x0028 }
        if (r8 == 0) goto L_0x004f;
    L_0x0043:
        r8 = r14.folder;	 Catch:{ all -> 0x0028 }
        r8 = r8.getStore();	 Catch:{ all -> 0x0028 }
        r8 = (com.sun.mail.pop3.POP3Store) r8;	 Catch:{ all -> 0x0028 }
        r8 = r8.forgetTopHeaders;	 Catch:{ all -> 0x0028 }
        if (r8 == 0) goto L_0x0074;
    L_0x004f:
        r8 = new javax.mail.internet.InternetHeaders;	 Catch:{ all -> 0x0028 }
        r8.<init>(r7);	 Catch:{ all -> 0x0028 }
        r14.headers = r8;	 Catch:{ all -> 0x0028 }
        r0 = r7;
        r0 = (javax.mail.internet.SharedInputStream) r0;	 Catch:{ all -> 0x0028 }
        r8 = r0;
        r10 = r8.getPosition();	 Catch:{ all -> 0x0028 }
        r8 = (int) r10;	 Catch:{ all -> 0x0028 }
        r14.hdrSize = r8;	 Catch:{ all -> 0x0028 }
    L_0x0061:
        r7 = (javax.mail.internet.SharedInputStream) r7;	 Catch:{ all -> 0x0028 }
        r8 = r14.hdrSize;	 Catch:{ all -> 0x0028 }
        r10 = (long) r8;	 Catch:{ all -> 0x0028 }
        r12 = -1;
        r8 = r7.newStream(r10, r12);	 Catch:{ all -> 0x0028 }
        r14.contentStream = r8;	 Catch:{ all -> 0x0028 }
    L_0x006e:
        monitor-exit(r14);	 Catch:{ all -> 0x0028 }
        r8 = super.getContentStream();
        return r8;
    L_0x0074:
        r6 = 0;
    L_0x0075:
        r5 = 0;
    L_0x0076:
        r2 = r7.read();	 Catch:{ all -> 0x0028 }
        if (r2 >= 0) goto L_0x008e;
    L_0x007c:
        r8 = r7.available();	 Catch:{ all -> 0x0028 }
        if (r8 != 0) goto L_0x00ab;
    L_0x0082:
        r0 = r7;
        r0 = (javax.mail.internet.SharedInputStream) r0;	 Catch:{ all -> 0x0028 }
        r8 = r0;
        r10 = r8.getPosition();	 Catch:{ all -> 0x0028 }
        r8 = (int) r10;	 Catch:{ all -> 0x0028 }
        r14.hdrSize = r8;	 Catch:{ all -> 0x0028 }
        goto L_0x0061;
    L_0x008e:
        if (r2 == r13) goto L_0x007c;
    L_0x0090:
        r8 = 13;
        if (r2 != r8) goto L_0x00a8;
    L_0x0094:
        r8 = r7.available();	 Catch:{ all -> 0x0028 }
        if (r8 <= 0) goto L_0x007c;
    L_0x009a:
        r8 = 1;
        r7.mark(r8);	 Catch:{ all -> 0x0028 }
        r8 = r7.read();	 Catch:{ all -> 0x0028 }
        if (r8 == r13) goto L_0x007c;
    L_0x00a4:
        r7.reset();	 Catch:{ all -> 0x0028 }
        goto L_0x007c;
    L_0x00a8:
        r5 = r5 + 1;
        goto L_0x0076;
    L_0x00ab:
        if (r5 != 0) goto L_0x0075;
    L_0x00ad:
        goto L_0x0082;
    L_0x00ae:
        r4 = move-exception;
        r8 = new javax.mail.MessagingException;
        r9 = "error fetching POP3 content";
        r8.<init>(r9, r4);
        throw r8;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.sun.mail.pop3.POP3Message.getContentStream():java.io.InputStream");
    }

    public synchronized void invalidate(boolean invalidateHeaders) {
        this.content = null;
        this.contentStream = null;
        this.msgSize = -1;
        if (invalidateHeaders) {
            this.headers = null;
            this.hdrSize = -1;
        }
    }

    public InputStream top(int n) throws MessagingException {
        try {
            InputStream top;
            synchronized (this) {
                top = this.folder.getProtocol().top(this.msgnum, n);
            }
            return top;
        } catch (EOFException eex) {
            this.folder.close(false);
            throw new FolderClosedException(this.folder, eex.toString());
        } catch (IOException ex) {
            throw new MessagingException("error getting size", ex);
        }
    }

    public String[] getHeader(String name) throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getHeader(name);
    }

    public String getHeader(String name, String delimiter) throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getHeader(name, delimiter);
    }

    public void setHeader(String name, String value) throws MessagingException {
        throw new IllegalWriteException("POP3 messages are read-only");
    }

    public void addHeader(String name, String value) throws MessagingException {
        throw new IllegalWriteException("POP3 messages are read-only");
    }

    public void removeHeader(String name) throws MessagingException {
        throw new IllegalWriteException("POP3 messages are read-only");
    }

    public Enumeration getAllHeaders() throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getAllHeaders();
    }

    public Enumeration getMatchingHeaders(String[] names) throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getMatchingHeaders(names);
    }

    public Enumeration getNonMatchingHeaders(String[] names) throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getNonMatchingHeaders(names);
    }

    public void addHeaderLine(String line) throws MessagingException {
        throw new IllegalWriteException("POP3 messages are read-only");
    }

    public Enumeration getAllHeaderLines() throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getAllHeaderLines();
    }

    public Enumeration getMatchingHeaderLines(String[] names) throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getMatchingHeaderLines(names);
    }

    public Enumeration getNonMatchingHeaderLines(String[] names) throws MessagingException {
        if (this.headers == null) {
            loadHeaders();
        }
        return this.headers.getNonMatchingHeaderLines(names);
    }

    public void saveChanges() throws MessagingException {
        throw new IllegalWriteException("POP3 messages are read-only");
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void loadHeaders() throws javax.mail.MessagingException {
        /*
        r8 = this;
        r7 = 0;
        monitor-enter(r8);	 Catch:{ EOFException -> 0x0030, IOException -> 0x0050 }
        r4 = r8.headers;	 Catch:{ all -> 0x002d }
        if (r4 == 0) goto L_0x0008;
    L_0x0006:
        monitor-exit(r8);	 Catch:{ all -> 0x002d }
    L_0x0007:
        return;
    L_0x0008:
        r3 = 0;
        r4 = r8.folder;	 Catch:{ all -> 0x002d }
        r4 = r4.getStore();	 Catch:{ all -> 0x002d }
        r4 = (com.sun.mail.pop3.POP3Store) r4;	 Catch:{ all -> 0x002d }
        r4 = r4.disableTop;	 Catch:{ all -> 0x002d }
        if (r4 != 0) goto L_0x0024;
    L_0x0015:
        r4 = r8.folder;	 Catch:{ all -> 0x002d }
        r4 = r4.getProtocol();	 Catch:{ all -> 0x002d }
        r5 = r8.msgnum;	 Catch:{ all -> 0x002d }
        r6 = 0;
        r3 = r4.top(r5, r6);	 Catch:{ all -> 0x002d }
        if (r3 != 0) goto L_0x0042;
    L_0x0024:
        r0 = r8.getContentStream();	 Catch:{ all -> 0x002d }
        r0.close();	 Catch:{ all -> 0x002d }
    L_0x002b:
        monitor-exit(r8);	 Catch:{ all -> 0x002d }
        goto L_0x0007;
    L_0x002d:
        r4 = move-exception;
        monitor-exit(r8);	 Catch:{ all -> 0x002d }
        throw r4;	 Catch:{ EOFException -> 0x0030, IOException -> 0x0050 }
    L_0x0030:
        r1 = move-exception;
        r4 = r8.folder;
        r4.close(r7);
        r4 = new javax.mail.FolderClosedException;
        r5 = r8.folder;
        r6 = r1.toString();
        r4.<init>(r5, r6);
        throw r4;
    L_0x0042:
        r4 = r3.available();	 Catch:{ all -> 0x002d }
        r8.hdrSize = r4;	 Catch:{ all -> 0x002d }
        r4 = new javax.mail.internet.InternetHeaders;	 Catch:{ all -> 0x002d }
        r4.<init>(r3);	 Catch:{ all -> 0x002d }
        r8.headers = r4;	 Catch:{ all -> 0x002d }
        goto L_0x002b;
    L_0x0050:
        r2 = move-exception;
        r4 = new javax.mail.MessagingException;
        r5 = "error loading POP3 headers";
        r4.<init>(r5, r2);
        throw r4;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.sun.mail.pop3.POP3Message.loadHeaders():void");
    }
}
